package com.cskaoyan.service;

import com.cskaoyan.bean.baseresultVO.BaseData;
import com.cskaoyan.bean.baseresultVO.BaseParam;
import com.cskaoyan.bean.domain.Log;
import com.cskaoyan.bean.domain.LogExample;
import com.cskaoyan.mapper.LogMapper;
import com.cskaoyan.utils.MyUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author looper
 * @
 **/
@Service
public class LogServiceImpl implements LogService {
    @Resource
    LogMapper mapper;

    @Override
    public BaseData getList(BaseParam baseParam, Log log) {
        //分页查询 并且通过log有值 就模糊查询
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        LogExample logExample = new LogExample();
        if (!MyUtils.isEmpty(log.getAdmin())) {
            //不为空的时候模糊查询
            logExample.createCriteria().andAdminLike("%" + log.getAdmin() + "%");
        }
        //排序
        logExample.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());
        List<Log> logList = mapper.selectByExample(logExample);
        PageInfo<Log> pageInfo = new PageInfo<>(logList);
        return BaseData.getBack(pageInfo.getSize(), pageInfo.getPageNum(), pageInfo.getPages(), MyUtils.getInteger(pageInfo.getTotal()), logList);
    }

    @Override
    public Integer insertLog(Log log) {
        log.setId(null);
        mapper.insertSelective(log);

        return 200;


    }

    @Override
    public Integer updateLoginResultStatus(Log log) {
        LogExample logExample = new LogExample();
        logExample.createCriteria().andIdEqualTo(log.getId());
        mapper.updateByExampleSelective(log, logExample);
        return 200;
    }
}
